/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.base.controller;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.xy.biz.base.client.QrCodeClient;
import com.xy.biz.base.domain.bo.QrCodeBO;
import com.xy.biz.base.domain.req.*;
import com.xy.biz.base.domain.resp.ListQrCodeResponse;
import com.xy.tool.util.QRCodeGenerateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.base.domain.resp.GetQrCodeResponse;
import com.xy.biz.base.domain.query.QrCodeQuery;
import com.xy.biz.base.domain.converter.QrCodeConverter;
import com.xy.biz.base.service.QrCodeService;

/**
 *
 *
 * @author zgy
 * @date 2021-8-11
 */
@Api(tags = "二维码服务")
@Slf4j
@RestController
@RequestMapping("/api/base/qr_code")
public class QrCodeController implements QrCodeClient {

    @Autowired
    private QrCodeService qrCodeService;

    @ApiOperation("生成二维码")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    @Override
    public Result<?> saveQrCode(@RequestBody SaveQrCodeRequest request) {
        return qrCodeService.saveQrCode(QrCodeConverter.toBO(request));
    }

    @ApiOperation("删除单条二维码")
    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteQrCode(@RequestBody GetQrCodeRequest request) {
        return Result.success(qrCodeService.deleteQrCode(request.getQrCodeId()));
    }

    @Validatable
    @PostMapping(value = "/get")
    public Result<GetQrCodeResponse> getQrCode(@RequestBody GetQrCodeRequest request) {
        QrCodeBO qrCode = qrCodeService.getQrCode(request.getQrCodeId());
        GetQrCodeResponse getQrCodeResponse = QrCodeConverter.toResponse(qrCode);
        getQrCodeResponse.setQrCode(QRCodeGenerateUtils.toBase64(qrCode.getQrCode()));
        return Result.success(getQrCodeResponse);
    }

    @Validatable
    @Override
    @PostMapping(value = "/getCode")
    public Result<?> getQrCode(@RequestBody GetQrCodeBytextRequest request) {
        QrCodeBO qrCode = qrCodeService.getQrCodeByText(QrCodeConverter.toBO(request));
        if (Objects.isNull(qrCode)) {
            return Result.success(0);
        }
        GetQrCodeResponse getQrCodeResponse = QrCodeConverter.toResponse(qrCode);
        getQrCodeResponse.setQrCode(QRCodeGenerateUtils.toBase64(qrCode.getQrCode()));
        return Result.success(getQrCodeResponse);
    }

    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<ListQrCodeResponse>> listQrCodeById(@RequestBody Collection<Long> collection) {
        return Result.success(QrCodeConverter.toResponse1(qrCodeService.listQrCodeById(collection)));
    }

    @ApiOperation("获取全部二维码")
    @Validatable
    @Override
    @PostMapping(value = "/list")
    public Result<Page<GetQrCodeResponse>> listQrCode(@RequestBody ListQrCodeRequest request) {
        QrCodeQuery query = QrCodeConverter.toQuery(request);
        List<QrCodeBO> qrCodeBOS = qrCodeService.listQrCode(query);
        Map<Long, byte[]> map = qrCodeBOS.stream().collect(Collectors.toMap(QrCodeBO::getQrId, QrCodeBO::getQrCode));
        List<GetQrCodeResponse> getQrCodeResponses = QrCodeConverter.toResponse(qrCodeBOS);
        getQrCodeResponses = getQrCodeResponses.stream().peek(q -> q.setQrCode(QRCodeGenerateUtils.toBase64(map.get(q.getQrId())))).collect(Collectors.toList());
        int rows = qrCodeService.countQrCode(query);
        return Result.success(new Page<>(getQrCodeResponses, rows));
    }

    @PostMapping(value = "/count")
    public Result<Integer> countQrCode(@RequestBody ListQrCodeRequest request) {
        return Result.success(qrCodeService.countQrCode(QrCodeConverter.toQuery(request)));
    }

/*    @PostMapping(value = "/page")
    public Result<Page<GetQrCodeResponse>> pageQrCode(@RequestBody ListQrCodeRequest request) {
        QrCodeQuery query = QrCodeConverter.toQuery(request);
        List<GetQrCodeResponse> list = QrCodeConverter.toResponse(qrCodeService.listQrCode(query));
        int count = qrCodeService.countQrCode(query);
        return Result.success(new Page<>(list, count));
    }*/

}
